package com.collect.tanzania.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiHistory;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.tanzania.R;
import com.collect.tanzania.zhiAdater.zhiLevelDetail.ZHILevelDetailGroupHistory;
import com.collect.tanzania.zhiBase.BaseActivity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRPerfermanceGroupHistoryEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiHelper.ZHIMainProcessHelper;
import com.collect.tanzania.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiSort.ZHIGroupHistoryCompareByDateASC;
import com.collect.tanzania.zhiScene.zhiWidget.zhiNavigationBar.ZHINavigationBar;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_PERFORMANCE_GROUP_AMOUNT_HISTORY)
public class ZHIPerformanceGroupAmountHistoryActivity extends BaseActivity<ZHIPerformanceGroupHistoryPresenter> implements ZHIPerformanceGroupHistoryContract.View {

    @BindView(R.id.navigationBar)
    ZHINavigationBar navigationBar;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    private final ZHILevelDetailGroupHistory mZHILevelDetailGroupHistory = new ZHILevelDetailGroupHistory();

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_perfermance_group_amount_history;
    }

    @Override
    protected void initView() {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            navigationBar.hideRight().setCenterText(getResources().getString(R.string.history)).setBarClickListener(new ZHINavigationBar.OnNavigationBarClickListener() {
                @Override
                public void onNavigationBarLeftClick(View v) {
                    finish();
                }

                @Override
                public void onNavigationBarRightClick(View v) {
                }
            });

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetMyGroupHistoryBonus(ArrayList<ZHIRPerfermanceGroupHistoryEntity> zhirPerfermanceGroupHistoryEntityArrayList, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            Collections.sort(zhirPerfermanceGroupHistoryEntityArrayList, new ZHIGroupHistoryCompareByDateASC());
            mZHILevelDetailGroupHistory.setNewData(zhirPerfermanceGroupHistoryEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        mZHILevelDetailGroupHistory.setEmptyView(getRefreshEmptyView());

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHILevelDetailGroupHistory);
    }

    public void initData(boolean showDialog) {
        ZHISPerfermanceEntity zhisPerfermanceEntity = getCaseSearchParams();
        assert mPresenter != null;
        mPresenter.getMyGroupHistoryBonus(getActivity(), zhisPerfermanceEntity, showDialog);
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISPerfermanceEntity getCaseSearchParams() {
        //return mZHIRootGroupFragmentInterface.onGetPerfermanceFilterParams();
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeaderUserId(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserId());
        return zhisPerfermanceEntity;
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }
}
